package com.example.testdemoutils.str;

import org.apache.logging.log4j.util.Strings;

import java.util.Objects;

public class DifferenceStrUtil {
    public static void main(String[] args) {
        String hear = "Hi My name is Deepak"
                + "\nHow are you How is everyone";
        String dear = "Hi My name is Deepak"
                + "\nHow are you Hey there \nHow is everyone";
        String translateText = difference(hear, dear);
        System.out.println(translateText);
    }

    public static String difference(String str1, String str2) {
        if (str1 == null) {
            return str2;
        }
        if (str2 == null) {
            return str1;
        }
        int at = indexOfDifference(str1, str2);
        if (at == -1) {
            return Strings.EMPTY;
        }
        return str2.substring(at);
    }

    public static int indexOfDifference(String str1, String str2) {
        if (Objects.equals(str1, str2)) {
            return -1;
        }
        if (str1 == null || str2 == null) {
            return 0;
        }
        int i;
        for (i = 0; i < str1.length() && i < str2.length(); ++i) {
            if (str1.charAt(i) != str2.charAt(i)) {
                break;
            }
        }
        if (i < str2.length() || i < str1.length()) {
            return i;
        }
        return -1;
    }
}
